<template>
  <div id="addPicturePage">
    <!--    标题-->
    <h2 style="margin-bottom: 16px">{{ route.query?.id ? '修改图片' : '创建图片' }}</h2>
    <!--    上传图片-->
    <!--    选择上传方式-->
    <a-tabs v-model:activeKey="uploadKey">
      <a-tab-pane key="file" tab="文件上传">
        <PictureUpload :picture="picture" :on-success="onSuccess" />
      </a-tab-pane>
      <a-tab-pane key="url" tab="URL上传">
        <UrlPictureUpload :picture="picture" :on-success="onSuccess" />
      </a-tab-pane>
    </a-tabs>

    <!--    图片信息表单-->
    <a-form layout="vertical" :model="pictureForm" @finish="doSubmit" v-if="picture">
      <a-form-item label="名称" name="name">
        <a-input v-model:value="pictureForm.name" placeholder="请输入图片名称" />
      </a-form-item>
      <a-form-item label="描述" name="introduction">
        <a-textarea
          v-model:value="pictureForm.introduction"
          :rows="2"
          autoSize
          allowClear
          showCount
          placeholder="请输入图片描述"
        />
      </a-form-item>
      <a-form-item label="分类" name="category">
        <a-auto-complete
          placeholder="请输入图片分类"
          :options="categoryOptions"
          v-model:value="pictureForm.category"
          allow-clear
        />
      </a-form-item>
      <a-form-item label="标签" name="tags">
        <a-select
          v-model:value="pictureForm.tags"
          mode="tags"
          :options="tagOptions"
          placeholder="请输入图片标签"
          allow-clear
        />
      </a-form-item>
      <a-form-item>
        <a-button type="primary" html-type="submit" style="width: 100%">创建</a-button>
      </a-form-item>
    </a-form>
  </div>
</template>
<script setup lang="ts">
import PictureUpload from '@/pages/PictureUpload.vue'
import { onMounted, reactive, ref } from 'vue'
import {
  editPictureUsingPost,
  getPictureVoByIdUsingGet,
  listPictureTagCategoryUsingGet,
} from '@/api/tupianguanli.ts'
import { message } from 'ant-design-vue'
import { useRoute, useRouter } from 'vue-router'
import UrlPictureUpload from '@/pages/UrlPictureUpload.vue'

const router = useRouter()
const route = useRoute()
const picture = ref<API.PictureVO>()
const pictureForm = reactive<API.PictureEditRequest>({})
const categoryOptions = ref<any>([])
const tagOptions = ref<any>([])
const uploadKey = ref<'file' | 'url'>('file')

onMounted(() => {
  getTagCategoryOptions()
  getOldPicture()
})
/**
 * 图片上传成功
 * @param newPicture
 */
const onSuccess = (newPicture: API.PictureVO) => {
  picture.value = newPicture
  pictureForm.name = newPicture.name
}
/**
 * 提交表单
 *@param values
 */
const doSubmit = async (values: any) => {
  const pictureId = picture.value?.id
  if (!pictureId) {
    return
  }
  const res = await editPictureUsingPost({
    id: pictureId,
    ...values,
  })
  if (res.data.code === 0 && res.data.data) {
    message.success('创建成功')
    router.push({
      path: `/picture/${pictureId}`,
      replace: true,
    })
  } else {
    message.error('创建失败' + res.data?.message)
  }
}
/**
 * 获取标签分类
 */
const getTagCategoryOptions = async () => {
  const res = await listPictureTagCategoryUsingGet()
  if (res.data.code === 0 && res.data.data) {
    tagOptions.value = (res.data.data.tagList ?? []).map((tag: string) => {
      return {
        label: tag,
        value: tag,
      }
    })
    categoryOptions.value = (res.data.data.categoryList ?? []).map((category: string) => {
      return {
        label: category,
        value: category,
      }
    })
  } else {
    message.error('获取标签分类失败' + res.data?.message)
  }
}
const getOldPicture = async () => {
  const id = route.query?.id
  if (id) {
    const res = await getPictureVoByIdUsingGet({
      id: id as any,
    })
    if (res.data.code === 0 && res.data.data) {
      picture.value = res.data.data
      pictureForm.name = res.data.data.name
      pictureForm.introduction = res.data.data.introduction
      pictureForm.category = res.data.data.category
      pictureForm.tags = res.data.data.tags
    } else {
      message.error('获取图片失败' + res.data?.message)
    }
  }
}
</script>
<style scoped>
#addPicturePage {
  max-width: 720px;
  margin: 0 auto;
}
</style>
